package com.example.java_gobang.game;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class RoomManager {
    private ConcurrentHashMap<String, Room> rooms = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer, String> userIdToRoomId = new ConcurrentHashMap<>();

    //添加房间
    public void add(Room room, Integer userId1, Integer userId2){
        rooms.put(room.getRoomId(), room);
        userIdToRoomId.put(userId1, room.getRoomId());
        userIdToRoomId.put(userId2, room.getRoomId());
    }

    //删除房间
    public void removeRoom(String roomId, Integer userId1, Integer userId2){
        rooms.remove(roomId);
        userIdToRoomId.remove(userId1);
        userIdToRoomId.remove(userId2);
    }

    //根据房间id查房间
    public Room getRoomByRoomId(String roomId){
        return rooms.get(roomId);
    }

    //根据学生id查房间
    public Room getRoomByUserId(Integer userId){
        String roomId = userIdToRoomId.get(userId);
        if(roomId == null){
            return null;
        }
        return rooms.get(roomId);
    }
}
